home *** CD-ROM | disk | FTP | other *** search
/ AppleScript - The Beta Release / AppleScript - The Beta Release.iso / Documentation / develop / Better Apple Event Coding / Code Samples / List.cp < prev    next >
Encoding:
Text File  |  1992-10-16  |  3.8 KB  |  185 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------------------
  2.  
  3.     Program:    CPlusTESample 2.0
  4.     File:        List.cp
  5.     Uses:       List.h
  6.  
  7.     by Andrew Shebanow
  8.     of Apple Macintosh Developer Technical Support
  9.  
  10.     Copyright © 1989-1990 Apple Computer, Inc.
  11.     All rights reserved.
  12.  
  13. ------------------------------------------------------------------------------------------*/
  14.  
  15. #ifndef __TYPES__
  16. #include <Types.h>
  17. #endif
  18. #ifndef __MEMORY__
  19. #include <Memory.h>
  20. #endif
  21. #ifndef __ERRORS__
  22. #include <Errors.h>
  23. #endif
  24.  
  25. #ifndef __EXCEPTIONS__
  26. #include "Exceptions.h"
  27. #endif
  28.  
  29. #include "List.h"
  30.  
  31. //----------------------------------------------------------------------------
  32.  
  33. TList::TList()
  34. {
  35.     fObjects = (ObjectHandle) NewHandle(sizeof(ObjectRef));
  36.  
  37.     fNumObjs = 0;
  38. }
  39.  
  40. void TList::InsertFirst(Object* obj)
  41. {
  42.     long lSize = GetHandleSize((Handle) fObjects);
  43.     SetHandleSize((Handle) fObjects, lSize+sizeof(ObjectRef));
  44.     FailMemError();
  45.     // shift list elements downwards
  46.     BlockMove((Ptr) &((*fObjects)[0]),
  47.               (Ptr) &((*fObjects)[1]),
  48.               fNumObjs*sizeof(ObjectRef));
  49.  
  50.     (*fObjects)[0] = obj;
  51.     fNumObjs++;
  52. }
  53.  
  54. void TList::InsertLast(Object* obj)
  55. {
  56.     long lSize = GetHandleSize((Handle) fObjects);
  57.     SetHandleSize((Handle) fObjects, lSize+sizeof(ObjectRef));
  58.     FailMemError();
  59.     (*fObjects)[fNumObjs] = obj;
  60.     fNumObjs++;
  61. }
  62.  
  63. void TList::MoveToFront(Object* obj)
  64. {
  65.     short i;
  66.  
  67.     // find object in list - return if not present or
  68.     // if object is already at front of list
  69.     if ((i = FindIdx(obj)) <= 0)
  70.       return;
  71.     // shift objects downwards
  72.     BlockMove((Ptr) &((*fObjects)[0]),
  73.               (Ptr) &((*fObjects)[1]),
  74.               (fNumObjs-i-1)*sizeof(ObjectRef));
  75.     (*fObjects)[0] = obj;
  76. }
  77.  
  78. void TList::MoveToBack(Object* obj)
  79. {
  80.     short i;
  81.  
  82.     // find object in list - return if not present or
  83.     // if object is already at back of list
  84.     if (((i = FindIdx(obj)) < 0) || (i == fNumObjs - 1))
  85.       return;
  86.     // shift objects upwards
  87.     BlockMove((Ptr) &((*fObjects)[i+1]),
  88.               (Ptr) &((*fObjects)[i]),
  89.               (fNumObjs-i-1)*sizeof(ObjectRef));
  90.     (*fObjects)[fNumObjs-1] = obj;
  91. }
  92.  
  93. void TList::MoveFront(Object* obj)
  94. {
  95.     short i;
  96.  
  97.     // find object in list - return if not present or
  98.     // if object is already at front of list
  99.     if ((i = FindIdx(obj)) <= 0)
  100.       return;
  101.     // swap the two objects in the list
  102.     Object* tObj = (*fObjects)[i-1];
  103.     (*fObjects)[i-1] = obj;
  104.     (*fObjects)[i] = tObj;
  105. }
  106.  
  107. void TList::MoveBack(Object* obj)
  108. {
  109.     short i;
  110.  
  111.     // find object in list - return if not present or
  112.     // if object is already at back of list
  113.     if (((i = FindIdx(obj)) < 0) || (i == fNumObjs - 1))
  114.       return;
  115.     // swap the two objects in the list
  116.     Object* tObj = (*fObjects)[i+1];
  117.     (*fObjects)[i+1] = obj;
  118.     (*fObjects)[i] = tObj;
  119. }
  120.  
  121. void TList::Remove(Object* obj)
  122. {
  123.     short i;
  124.  
  125.     // find object in list - return if not present
  126.     if ((i = FindIdx(obj)) < 0)
  127.       return;
  128.     // shift down if not the last element
  129.     if (i < (fNumObjs - 1))
  130.       BlockMove((Ptr) &((*fObjects)[i+1]),
  131.                 (Ptr) &((*fObjects)[i]),
  132.                 fNumObjs*sizeof(ObjectRef));
  133.     fNumObjs--;
  134.     SetHandleSize((Handle) fObjects, fNumObjs*sizeof(ObjectRef));
  135. }
  136.  
  137. void TList::RemoveAll()
  138. {
  139.     SetHandleSize((Handle) fObjects,0);
  140.     fNumObjs = 0;
  141. }
  142.  
  143. void TList::Delete(Object* obj)
  144. {
  145.     Remove(obj);
  146.     delete obj;
  147. }
  148.  
  149. void TList::DeleteAll()
  150. {
  151.     // this is kind of strange. We go through the
  152.     // list and delete all the objects BEFORE we
  153.     // actually get rid of the references in the
  154.     // list. This is ok, because we won't ever look at
  155.     // the values again
  156.     for (short i = 0; i < fNumObjs; i++)
  157.       delete (*fObjects)[i];
  158.     RemoveAll();
  159. }
  160.  
  161. Object* TList::At(short idx) const
  162. {
  163.     if ((idx < 0) || (idx > (fNumObjs-1)))
  164.       return nil;
  165.     return (*fObjects)[idx];
  166. }
  167.  
  168. short TList::FindIdx(Object* obj)
  169. {
  170.     for (short i = 0; i < fNumObjs; i++)
  171.       {
  172.         if ((*fObjects)[i] == obj)
  173.           return i;
  174.       }
  175.     return -1;
  176. }
  177.  
  178. //----------------------------------------------------------------------------
  179.  
  180. TListIterator::TListIterator(const TList* ls) : fList(ls)
  181. {
  182.     fIdx = 0;
  183. }
  184.  
  185.